//
//  BouncePresentAnimation.m
//  VCTransitionDemo
//
//  Created by 赵国腾 on 16/4/25.
//  Copyright © 2016年 赵国腾. All rights reserved.
//

#import "BouncePresentAnimation.h"

@implementation BouncePresentAnimation

- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.8;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    
    // 通过transitionContext获取目标控制器
//    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//    CGRect finalFrame = [transitionContext finalFrameForViewController:toVC];
//    CGRect initFrame = CGRectOffset(finalFrame, 0, finalFrame.size.height);
//    toVC.view.frame = initFrame;
    
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    CGRect finalFrame = toView.frame;
    CGRect initFrame = CGRectOffset(finalFrame, 0, finalFrame.size.height);
    toView.frame = initFrame;
    
    // 把视图添加到容器视图上面
    UIView *containerView = [transitionContext containerView];
    containerView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.2];
    [containerView addSubview:toView];
    
    // 设置动画时间
    CGFloat duration = [self transitionDuration:transitionContext];
    
    // 动画
    [UIView animateWithDuration:duration
                          delay:0.0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                            toView.frame = finalFrame;
//                         toView.transform = CGAffineTransformMakeScale(0.5, 0.5);
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}


@end



// 容器视图
//- (nullable UIView *)containerView;

//// Most of the time this is YES. For custom transitions that use the new UIModalPresentationCustom
//// presentation type we will invoke the animateTransition: even though the transition should not be
//// animated. This allows the custom transition to add or remove subviews to the container view.
//- (BOOL)isAnimated;
//
//- (BOOL)isInteractive; // This indicates whether the transition is currently interactive.
//
//- (BOOL)transitionWasCancelled;
//
//- (UIModalPresentationStyle)presentationStyle;
//
//// It only makes sense to call these from an interaction controller that
//// conforms to the UIViewControllerInteractiveTransitioning protocol and was
//// vended to the system by a container view controller's delegate or, in the case
//// of a present or dismiss, the transitioningDelegate.
//- (void)updateInteractiveTransition:(CGFloat)percentComplete;
//- (void)finishInteractiveTransition;
//- (void)cancelInteractiveTransition;
//
//// This must be called whenever a transition completes (or is cancelled.)
//// Typically this is called by the object conforming to the
//// UIViewControllerAnimatedTransitioning protocol that was vended by the transitioning
//// delegate.  For purely interactive transitions it should be called by the
//// interaction controller. This method effectively updates internal view
//// controller state at the end of the transition.
//- (void)completeTransition:(BOOL)didComplete;
//
//
//// Currently only two keys are defined by the
//// system - UITransitionContextToViewControllerKey, and
//// UITransitionContextFromViewControllerKey.
//// Animators should not directly manipulate a view controller's views and should
//// use viewForKey: to get views instead.
//- (nullable __kindof UIViewController *)viewControllerForKey:(NSString *)key;
//
//// Currently only two keys are defined by the system -
//// UITransitionContextFromViewKey, and UITransitionContextToViewKey
//// viewForKey: may return nil which would indicate that the animator should not
//// manipulate the associated view controller's view.
//- (nullable __kindof UIView *)viewForKey:(NSString *)key NS_AVAILABLE_IOS(8_0);
//
//- (CGAffineTransform)targetTransform NS_AVAILABLE_IOS(8_0);
//
//// The frame's are set to CGRectZero when they are not known or
//// otherwise undefined.  For example the finalFrame of the
//// fromViewController will be CGRectZero if and only if the fromView will be
//// removed from the window at the end of the transition. On the other
//// hand, if the finalFrame is not CGRectZero then it must be respected
//// at the end of the transition.
//- (CGRect)initialFrameForViewController:(UIViewController *)vc;
//- (CGRect)finalFrameForViewController:(UIViewController *)vc;
//@end
